﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelMove : MonoBehaviour
{
    [Header("关卡高度")]
    public float levelLength;
    [Header("关卡水平偏移")]
    public float levelOffset;
    [Header("关卡")]
    public GameObject[] roomPrefab;
    [Header("关卡移动速度")]
    public float velocity;
    [Header("关卡生成总数")]
    public float num;

    private Rigidbody2D rb;

    private int levelNum=1;//生成关卡数



    // Start is called before the first frame update
    void Awake()
    {
        levelNum = 1;
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        rb.velocity = Vector2.up * velocity;
    }


    /// 最右跑道移动至相机边界时调用，产生新关卡
    public void OnCreateLevel()
    {
        if (levelNum>num)
        {
            return;
        }

        GameObject newLevel = GameObject.Instantiate(RandomSelect(ref roomPrefab), Vector3.zero, Quaternion.identity, transform);
        newLevel.transform.localPosition = new Vector3(0, levelNum * levelLength + levelOffset, 0);
        levelNum += 1;
        //Debug.Log(levelNum * levelLength + levelOffset);
        //Debug.Log(levelNum);
    }

    public static T RandomSelect<T>(ref T[] array)
    {
        int num = Random.Range(0, array.Length);
        return array[num];
    }
}
